#include "global.h"
#include "kdtree.h"


int main()
{
	int numbound;
	Polygon bound;
	std::vector<Point> v_p;
	std::cin>>numbound;
	for (int i = 0; i < numbound; ++i)
	{
		Point p;
		std::cin>>p;
		bound.push_back(p);
	}
	
	int numseeds;
	std::cin>>numseeds;
	for (int i = 0; i < numseeds; ++i)
	{
		Point p;
		std::cin>>p;
		v_p.push_back(p);
	}
        
        
	Hierarchy obj(bound);
        for (int j = 0; j < v_p.size(); ++j)
        {
                obj.insert (v_p, j);
        }

	DOUBLE threshold = 1e-7;
	for (int i = 0; ; ++i)
	{

		vector <Point> old(v_p);
		//for (unsigned j = 0; j < old.size(); ++j)
		//{
		//	std::cout<<old[j]<<std::endl;
		//}	
		
		obj.hierarchy_tessellation(v_p);
                DOUBLE e = energy_function(v_p, old);
                std::cout<<"Energy     : "<<i<<' '<< e <<std::endl;	
                if (e < threshold) break;                		
	}
	//obj.output_result (v_p);
	/*
	std::cout<<v_p.size()<<std::endl;
	
	DOUBLE threshold = 1e-7;
	for (int j = 0; ; ++j)
	{
		CVT obj(bound,v_p);
		vector <Point> v_p_old (v_p);
		v_p.clear(); 
		for (unsigned i = 0; i < obj.size(); ++i)
		{
			Polygon face;
			Point cen;
			obj.convert_face_region(i, face);
			centroid(face, cen);
			v_p.push_back(cen);
			//std::cout<<p<<std::endl;
		}
                DOUBLE e = energy_function(v_p, v_p_old);
                std::cerr<<"Energy     : "<<j<<' '<< e <<std::endl;		
                if (e < threshold) break;
	}
	*/
	//std::vector<Point>  result;
	//std::vector<Polygon>  faces;
    	//lloyd(v_p,  bound, result,  faces, 1e-8);
	//simple_increment (v_p, bound, result, faces, 1e-7);
    	

	return 0;
}
